﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediSIIM.BizComponent.HangZhouYHQYBInterface;
using System.Diagnostics;
using MediSIIM.BizComponent.HangZhouYHQYB;

namespace YUHANGMLDZ
{
    public class Util
    {

        public static HangZhouYHQYBTradeParms YBCall(HangZhouYHQYBTradeParms callparam)
        {
            string addr = "10.81.193.1";
            string servlet = "Insur_App/ProcessAll";
            uint point = YiBaoJKDLL.newinterface();
            YiBaoJKDLL.init(point, ref addr, 80, ref servlet);
            YiBaoJKDLL.login(point);
            YiBaoJKDLL.enablelog(point);
            string result = string.Empty.PadRight(1024);
            int retcode = YiBaoJKDLL.start(point, (int)callparam.TradeNo);
            string log = "$I ";
            foreach (var item in callparam.ParamInfo.Columns)
            {
                //PUT基本参数
                log += item.Name + "=" + item.Value + "|";
                YiBaoJKDLL.put(point, 1, item.Name, item.Value);
                Debug.WriteLine(YiBaoJKDLL.getmessage(point));
            }
            log = log.TrimEnd('|');
            YiBaoJKDLL.writelog(log);
            foreach (var item in callparam.InputTables)
            {
                #region PUT数据集入参

                YiBaoJKDLL.setresultset(point, item);
                var rowlist = callparam[item];
                for (int i = 0; i < rowlist.Rows.Count; i++)
                {
                    for (int j = 0; j < rowlist[i].Columns.Count; j++)
                    {
                        var kv = rowlist[i][j];
                        log += kv.Name + "=" + kv.Value + "|";
                        YiBaoJKDLL.put(point, i + 1, kv.Name, kv.Value);
                    }
                }

                #endregion
            }
            var now = DateTime.Now;
            Debug.WriteLine("医保交易: " + now.ToString("yyyy-MM-dd HH:m:ss.fff"));
            callparam.RetCode = YiBaoJKDLL.run(point);
            Debug.WriteLine("交易结束,Time: " + (DateTime.Now - now).TotalSeconds + " 秒");
            result = YiBaoJKDLL.getmessage(point);
            if (callparam.RetCode >= 0)
            {
                GetTables(callparam, callparam.OutTables, point);
            }
            else
            {
                GetTables(callparam, callparam.FailTables, point);
            }
            YiBaoJKDLL.destoryinterface(point);
            if (callparam.RetCode < 0)
            {
                throw new Exception("医保中心: " + result);
            }
            return callparam;
        }

        private static void GetTables(HangZhouYHQYBTradeParms callparam, List<string> tables, uint point)
        {
            #region 获取数据集

            foreach (var item in tables)
            {
                var table = ParamTable.NewTable(item.ToLower());
                if (YiBaoJKDLL.setresultset(point, table.TableName) >= 0)
                {
                    do
                    {
                        int col = 1;
                        var row = ParamTable.NewParamRow();
                        while (true)
                        {
                            string name = string.Empty.PadRight(1024);
                            string value = string.Empty.PadRight(1024);
                            if (YiBaoJKDLL.getbyindex(point, col, ref name, ref value) < 0)
                            {
                                var msg = YiBaoJKDLL.getmessage(point);
                                if (msg == "错误的数据项名称." || msg == "行号越界." || col > 100)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                row[name.TrimEnd(' ', '\0')] = value.TrimEnd(' ', '\0');
                            }
                            col++;
                        }
                        if (row.Values.Count > 0)
                        {
                            table.AddRow(row);
                        }
                    } while (YiBaoJKDLL.nextrow(point) > 0);
                }
                callparam.OutputTable.Add(table.TableName, table);
            }
            #endregion
        }
    }
}
